<#assign ctx=request.contextPath />
<#import '${ctx}/sys/include/head.html' as d/>
<#import '${ctx}/sys/include/body.html' as c/>
<#import '${ctx}/sys/include/table/toolbar.html' as t/>
<!DOCTYPE html>
<html>
<head>
<@d.head title="账号列表" />
</head>
<@c.body>
<table class="layui-hide" id="user_list" lay-filter="user_list">
</@c.body>
<script>
layui.use(['table','appJs','dtree'],function(){
	var table = layui.table;
	var $ = layui.$;
	var app = layui.appJs;
	var dtree = layui.dtree;
	var tableOptions = {
		  	id:'user_table'
			    ,elem: '#user_list'
			    //,height: 420
			    ,url: '${ctx}/sys/user/user_list' //数据接口
			    ,title: '角色表'
			    ,page: true //开启分页
			    ,toolbar: '#topBar' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
			    ,cols: [[ //表头
			      {type: 'checkbox', fixed: 'left'}
			      ,{field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
			      ,{field: 'name',width:100, title: '用户名'}
			      ,{field: 'roleName',title: '角色名',templet:function(d){
			    	  if(d.roleNameDiscard==1){
			    		  return '(已废弃) '+d.roleName
			    	  }else {
			    		  return d.roleName==null?'无':d.roleName;
			    	  }
			    	}
			      }
			      ,{field: 'comment',title: '说明'}
			      ,{fixed: 'right',title:'操作', width:180, align:'center', toolbar: '#itemBar'}
			    ]]
			  };
	  table.render(tableOptions);
	  table.on('toolbar(user_list)',function(obj){
		  var checkStatus = table.checkStatus(obj.config.id);
		  switch(obj.event){
		    case 'add':
		      var info_layer = layer.open({
		    	  type:2
		    	  ,title:'添加角色'
		    	  ,maxmin: true//开启最大化最小化按钮
		    	  ,content:['${ctx}/sys/user/p/user_info']
		      	  ,success:function(layero, index){
		      			var iframeWin = window[layero.find('iframe')[0]['name']];
		      		}
		      	  ,end:function(){
		      		  table.reload('user_table',tableOptions);
		      	  }

		      });
		      layer.full(info_layer);
		    break;
		    case 'delete':
		    	if(checkStatus.data.length<=0) {
		    		layer.msg("未选中删除行");
		    		return;
		    	}
	    		layer.confirm('确定删除吗？', function(index){
	    			var ids = [];
	  		      for(var r of checkStatus.data) {
	  		    	  ids.push(r.id);
	  		      }
	  		      $.ajax({
	  		    	  type:'post',
	  		    	  url:'${ctx}/sys/user/deletes',
	  		    	  data:{ids:ids},
	  		    	  traditional: true,
	  		    	  success:function(data){
	  		    		  if(data.code == 0) {
	  		    		  	layer.msg("删除成功");
	  		    		  	table.reload('user_table',tableOptions);
	  		    		  }else {
	  		    			  layer.msg(data.code+":"+data.msg);
	  		    		  }
	  		    	  }
	  		      });
	    		});

		    break;
		  };
	  });

	  table.on('tool(user_list)',function(obj){
		  var data = obj.data; //获得当前行数据
		  var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
		  var tr = obj.tr; //获得当前行 tr 的DOM对象

		  if(layEvent === 'detail'){ //查看
		    var layerIndex = layer.open({
		    	type:2
		    	,title:'角色详情'
		    	,maxmin: true//开启最大化最小化按钮
		    	,content:['${ctx}/sys/user/p/user_info']
		      	,success:function(layero, index){
		      		var iframeWin = window[layero.find('iframe')[0]['name']];
		      		app.fillForm($(iframeWin.document).find('.layui-form'),'${ctx}/sys/user/info',data.id);
		      		app.disableForm($(iframeWin.document).find('.layui-form'));
		      		//iframeWin.disableEdit();
		      	}
		    });
		    layer.full(layerIndex);
		  } else if(layEvent === 'del'){ //删除
		    layer.confirm('真的删除行么', function(index){
		    	$.ajax({
	  		    	  type:'post',
	  		    	  url:'${ctx}/sys/user/delete',
	  		    	  data:{id:data.id},
	  		    	  success:function(data){
	  		    		  if(data.code == 0) {
	  		    		  	layer.msg("删除成功");
	  		    		  obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
	  				      layer.close(index);
	  		    		  }else {
	  		    			  layer.msg(data.code+":"+data.msg);
	  		    		  }
	  		    	  }
	  		      });
		    });
		  } else if(layEvent === 'edit'){ //编辑
		    //do something
		    	  var info_layer = layer.open({
		    	  type:2
		    	  ,title:'添加角色'
		    	  ,maxmin: true//开启最大化最小化按钮
		    	  ,content:['${ctx}/sys/user/p/user_info']
		      	  ,success:function(layero, index){
		      		var iframeWin = window[layero.find('iframe')[0]['name']];
		      		$(iframeWin.document).find('.layui-form').append('<input name="id" type="hidden" />');
		      		app.fillForm($(iframeWin.document).find(".layui-form"),'${ctx}/sys/user/info',data.id);
		      		iframeWin.layui.form.render();
		      		//iframeWin.disableEdit();
		      	  },
  		    	  end:function(){
		      		  table.reload('user_table',tableOptions);
		      	  }
		      });
		      layer.full(info_layer);
		    //同步更新缓存对应的值
		    obj.update({
		      username: '123'
		      ,title: 'xxx'
		    });
		  }
	  });

})
</script>
<@t.bar />